package mq

import (
	"context"

	"github.com/lab-online/internal/report/entity"
	"github.com/lab-online/pkg/mq"
	amqp "github.com/rabbitmq/amqp091-go"
)

const (
	reportContentQueue    = "report.content"
	reportPublishExchange = "report.publish"
)

type MQ struct {
	*mq.MessageQueue
	publishReportChannel *amqp.Channel
}

type ReportMQ interface {
	// ConsumeContent 处理实验返回结果
	ConsumeContent(context.Context, func(*mq.Delivery)) error
	PublishReport(report entity.ReportEntity) error
	SubscribeReport(ctx context.Context, reportID uint, body chan<- []byte) error
}

func New(rabbitMQ *mq.MessageQueue) ReportMQ {
	messageQueue := &MQ{MessageQueue: rabbitMQ}

	var err error
	if messageQueue.publishReportChannel, err = messageQueue.DeclareExchange(
		mq.ExchangeDeclareConfig{
			Name:    reportPublishExchange,
			Kind:    "direct",
			Durable: true,
		},
	); err != nil {
		panic(err)
	}

	return messageQueue
}
